(function () {
  new Vue({
    el: '#commissionDetail',
    data() {
      return {
        date: '',
        detailData: {
          billState: '',
        },
        isShowSettle: false,
        billId: hlUrlUtil.GetQueryString('id'),
        turnover_amount: '',//营业额确认
        isFlag: false,
        commissionDetail: {
          commissionRuleInfoList: [],
          totalAmount: '',
          turnoverAmount: '',
        },//提交营业额获取的抽成数据
        paymentDeadline: '',
        unitList: [],//单元列表unitList
        acreage: 0,//单元面积
        permissions: JSON.parse(localStorage.getItem('permissions')),
        HlValidator: ''
      }
    },
    watch: {
      date() {
        this.saveCountMes()
      }
    },
    filters: {
      formatDate: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      toFixed2: function (n) {
        return Number(n).toFixed(2)
      },
      //千分位
      formatNum: function (n) {
        return hlNumberUtil.formatNum(n)
      },
    },
    mounted() {
      this.getCommissionDetail();
      this.HlValidator = new HlValidator()
    },
    methods: {
      //获取抽成详情
      getCommissionDetail() {
        var that = this
        hlhttp.get({
          url: 'bill/commission',
          data: { bill_id: this.billId },
          success: (res) => {
            if (res.commissonConfirmFlag == 'YTJ') {
              that.commissionDetail.commissionRuleInfoList = res.commissionRuleInfoList
              that.commissionDetail.totalAmount = res.totalAmount
              that.commissionDetail.turnoverAmount = res.turnoverAmount
            }
            that.detailData = res
            that.date = hlDateUtil.formatUTCDate(res.paymentDeadline, 'yyyy-MM-dd')
            that.getUnit(res.contractId);
          }
        })
      },
      // 获取单元详情
      getUnit(id) {
        var that = this
        hlhttp.get({
          url: 'contract/units',
          data: { id: id },
          success: (res) => {
            res.map(item => {
              that.acreage = that.acreage + item.unitAcreage
            })
            that.unitList = res
          }
        })
      },
      //提交营业额
      amountSubmit() {
        this.HlValidator.add(this.turnover_amount, [
          {
            rule: 'isEmpty',
            msg: '请输入金额'
          },
          {
            rule: 'isAmount',
            msg: '请输入正确金额'
          }])
        var errorMsg = this.HlValidator.start()
        if (errorMsg != true) {
          hl.toast(errorMsg)
          return false
        }
        var that = this
        hlhttp.put({
          url: 'bill/turnover_amount/_submit',
          data: {
            bill_id: this.billId,
            turnover_amount: this.turnover_amount
          },
          success: (res) => {
            that.commissionDetail = res
          }
        })
      },
      // 抽成按钮
      commissionBut() {
        this.isFlag = true
      },
      // 确定提交抽成
      commissionSubmit() {
        let commission = []
        this.commissionDetail.commissionRuleInfoList.map(item => {
          commission.push({
            commission_rule_id: item.commissionRuleId,
            base_val_down_limit: item.baseValDownLimit ? item.baseValDownLimit + '' : '#',
            base_val_up_limit: item.baseValUpLimit ? item.baseValUpLimit + '' : '#',
            commission_percent: item.commissionPercent ? item.commissionPercent + '' : '#',
            commission_down_limit: item.commissionDownLimit ? item.commissionDownLimit + '' : '#',
            commission_up_limit: item.commissionUpLimit ? item.commissionUpLimit + '' : '#',
            late_fee_percent: item.lateFeePercent ? item.lateFeePercent + '' : '#',
            commission_rule_amount: item.commissionAmount ? item.commissionAmount + '' : '#',
          })
        })
        hlhttp.put({
          url: 'bill/commission/_submit',
          data: {
            "bill_id": this.billId,
            "total_commission_rule_amount": this.commissionDetail.totalAmount,
            "turnover_amount": this.commissionDetail.turnoverAmount,
            "commission": JSON.stringify(commission)
          },
          success: (res) => {
            hl.toast('提交成功')
            this.isFlag = false
            this.goToCommission();
          }
        })
      },
      // 修改收款截止日
      saveCountMes: function () {
        if (this.detailData.billState != 'WTJ') return
        hlhttp.put({
          url: 'bill/payment_deadline',
          data: {
            bill_id: this.billId,
            payment_deadline: this.date
          },
          success: () => {

          }
        })
      },
      // 获取营业额输入焦点
      inputFocus() {
        this.commissionDetail = {
          commissionRuleInfoList: [],
          totalAmount: '',
          turnoverAmount: '',
        }
      },
      // 前往账单详情
      goToBill() {
        hlUrlUtil.goTo('billDetail', { id: this.billId, aid: this.detailData.contractId, ddtab: true }, true)
      },
      // 前往抽成管理
      goToCommission() {
        hlUrlUtil.goTo('commissionManagement', { ddtab: true }, true)
      },
      //前往营业额详情
      goToTurnover(code, id) {
        if (code != 'WIPT') return
        hlUrlUtil.goTo('turnoverDetail', { id: id, ddtab: true }, true)
      },
      //前往合同详情
      goToContract() {
        hlUrlUtil.goTo('contractNewDeatil', { id: this.detailData.contractId, ddtab: true }, true)
      }
    },
  })
}())
